package com.lry.web.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lry.web.config.RabbitMQConfig;
import com.lry.web.video.entity.Video;
import com.lry.web.video.mapper.VideoMapper;
import com.lry.web.video.service.VideoChapterService;
import com.lry.web.video.service.VideoService;
import com.lry.web.video.service.VideoUpdateService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    VideoMapper videoMapper;

    @Autowired
    VideoChapterService videoChapterService;
    @Autowired
    VideoUpdateService videoUpdateService;






    //根据条件在mysql查询视频
    public Map<String,Object> getVideos(String key, String option, Integer from, Integer size) throws IOException {
        Page<Video> page = new Page<Video>(from,size);
        QueryWrapper wrapper = new QueryWrapper();
        if("title".equals(option)){
            wrapper.like("title",key);
            wrapper.orderByAsc("length(title)");
        }else{
            wrapper.like(option,key);
            wrapper.orderByDesc("update_time");
        }
        videoMapper.selectPage(page,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("data",page.getRecords());
        map.put("count",page.getTotal());
        return map;
    }


    //根据VVId删除视频,效率非常低,不推荐使用
    public int deleteByVVId(String vVId) throws Exception {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("video_variable_id",vVId);
        videoChapterService.deleteByVVId(vVId);
        videoUpdateService.deleteByVVId(vVId);

        return videoMapper.delete(wrapper);
    }



    //发起采集请求到MQ
    public int spiderVideo(String title) throws InterruptedException {
        rabbitTemplate.convertAndSend(RabbitMQConfig.GATHEREXCHANGE_NAME,"video.gather",title);
        Thread.sleep(1000);
        while(RabbitMQConfig.code==0){
            Thread.sleep(1000);
        }
        int i = RabbitMQConfig.code;
        RabbitMQConfig.code = 0;
        return i;
    }
    //获取视频总数量
    public Integer selectNum() {
        return videoMapper.selectNum();
    }

    //获取全部视频，分页
    public Map<String,Object> selectVideos(int from, int size,String option, String key) {
        Page<Video> page = new Page<Video>(from,size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("update_time");
        if(option !=null && key != null){
            wrapper.like(option,key);
        }
        page.toString();
        videoMapper.selectPage(page, wrapper);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("videos",page.getRecords());
        map.put("pages",page.getPages());
        return map;
    }

    //更具VId删除视频
    public int deleteByVideoId(String videoId) throws Exception {
        List<String> listVId = Arrays.asList(videoId.split(","));
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("video_id",listVId);
        videoChapterService.deleteByVideoId(videoId);
        videoUpdateService.deleteByVideoId(videoId);
        return videoMapper.delete(wrapper);
    }

}
